$(function () {
  $("#uploadForm").submit(upload);
})

function upload() {
    console.log(new FormData($("#uploadForm")[0]))
  $.ajax({
    url:"http://upload-z2.qiniup.com",
    method : "post",
    processData: false,
    contentType: false,
    data: new FormData($("#uploadForm")[0]),
    success : function(data) {
      if(data && data.code == 0) {
        // 更新头像访问路径
        $.post(
            CONTEXT_PATH + "/user/header/url",
            {"fileName":$("input[name='key']").val()},
            function(data) {
              data = $.parseJSON(data);
              if(data.code == 0) {
                window.location.reload();
              } else {
                alert(data.msg);
              }
            }
        );
      } else {
        alert("上传失败!");
      }
    },
      error : function (data){
        alert("访问七牛云服务器失败！")
      }
  })

}

function updatePassword(){
    oldPassword = $("[name='oldPassword']").val();
    newPassword = $("[name='newPassword']").val();
    $.ajax({
        url: CONTEXT_PATH + "/user/password",
        type: "GET",
        data: {"oldPassword": oldPassword,"newPassword": newPassword},
        dataType: "json",
        contentType: "JSON",
        success: function (data){
            if (data.code == 0){
                $.confirm({
                    title: '成功',
                    content: data.msg,
                    onContentReady: function () {
                        setTimeout(function () {
                            window.location.reload();
                        }, 2000);
                    },
                });
            }else{
                $.confirm({
                    title: '失败',
                    content: data.msg,
                    onContentReady: function () {
                        setTimeout(function () {
                            window.location.reload();
                        }, 2000);
                    },
                });
            }
        }
    })
}